//
//  ShowUserMailAddress.m
//  ELife
//
//  Created by 丰 璐 on 11-7-26.
//  Copyright 2011年 __MyCompanyName__. All rights reserved.
//

#import "ShowUserMailAddress.h"
#import "showUserMailCell.h"
#import "ELifeAppDelegate.h"
#import "AlertView.h"
#import "showShopCard.h"

@implementation ShowUserMailAddress
@synthesize myDic;


- (id)initWithStyle:(UITableViewStyle)style setDele:(id)target;
{
    self = [super initWithStyle:style];
    if (self) {
        // Custom initialization
    }
    delegate = target;
    return self;
}

- (void)dealloc
{
    [myDic release];
    [super dealloc];
}

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

- (void)viewDidLoad
{
    [super viewDidLoad];

    sce = -1;
    UIButton* button = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 100, 30)];
    [button setTitle:@"付款" forState:UIControlStateNormal];
    button.center = CGPointMake(160, 20);
    [button addTarget:self action:@selector(finishAction) forControlEvents:UIControlEventTouchUpInside];
    [self.tableView setTableFooterView:button];
    [button release];
//    [self.tableView setBackgroundView:[ELifeAppDelegate App].backImage];
    // Uncomment the following line to preserve selection between presentations.
    // self.clearsSelectionOnViewWillAppear = NO;
 
    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem;
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
}

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
}

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
}

- (void)viewDidDisappear:(BOOL)animated
{
    [super viewDidDisappear:animated];
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

#pragma mark - Table view data source

-(CGFloat) tableView:(UITableView*) tableView 
heightForRowAtIndexPath:(NSIndexPath*) indexPath
{
	return 148;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
#warning Potentially incomplete method implementation.
    // Return the number of sections.
    return [myDic count];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
#warning Incomplete method implementation.
    // Return the number of rows in the section.
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"showUserMailCell";
    
    showUserMailCell* cell2 =  (showUserMailCell*)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell2 == nil) {
        NSArray *nib = [[NSBundle mainBundle]loadNibNamed:@"mailAddressCell" owner:self options:nil];
        cell2 = [nib objectAtIndex:2];
    }
    if (sce != -1 && sce == [indexPath section]) {
        cell2.accessoryType = UITableViewCellAccessoryCheckmark;
    }
    cell2.selectionStyle = UITableViewCellSelectionStyleNone;
    NSDictionary* dic = [myDic objectAtIndex:[indexPath section]];
    
    [cell2.label1 setText:[dic objectForKey:@"mail_people"]];
    [cell2.label2 setText:[dic objectForKey:@"mail_address"]];
    [cell2.label3 setText:[dic objectForKey:@"mail_phone"]];
    [cell2.label4 setText:[dic objectForKey:@"mail_zipcode"]];
    
    
    return cell2;
}

/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/

/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
    }   
    else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath
{
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

#pragma mark - Table view delegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    sce = [indexPath section];
    [self.tableView reloadData];
}

-(void) finishAction
{
    if (sce > -1) {
        [self initWithActionSheet:[NSArray arrayWithObjects:@"余额支付", @"银行卡支付", nil] showInView:self.view];
    }else
    {
        [AlertView showAlert:@"请选择邮寄地址"];
    }
    
}


-(void) initWithActionSheet:(NSArray*)buttonTitle showInView:(UIView*)view
{

	UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:@""
															 delegate:self cancelButtonTitle:nil destructiveButtonTitle:nil otherButtonTitles:nil];
	int i = 0;
	for (; i<[buttonTitle count]; i++) {
		[actionSheet addButtonWithTitle:[buttonTitle objectAtIndex:i]];
	}
	actionSheet.actionSheetStyle = UIActionSheetStyleDefault;
	
    [actionSheet addButtonWithTitle:@"取消"];
	actionSheet.cancelButtonIndex = i;
	actionSheet.tag = 2022;
	[actionSheet showInView:view]; 
	[actionSheet release];
}

- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
    NSDictionary* dic = [myDic objectAtIndex:sce];
    if (buttonIndex == 0) {
        [delegate shopAccount_goodspay:[dic objectForKey:@"mail_id"]];
        
    }else if (buttonIndex == 1) {
        showShopCard* card = [showShopCard alloc];
        [card setDelegate:self];
        UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:card];
        [self presentModalViewController:navigationController animated:YES];
        [navigationController release];
        [card release];
    }
}


-(void) showShopCardID:(NSString*) cardId
{
    NSDictionary* dic = [myDic objectAtIndex:sce];
    [delegate shopMvpos_goodspay:[dic objectForKey:@"mail_id"] CardId:cardId];
}
@end
